﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Messaging
/// </summary>
public class Messaging
{
    User byUser;
	
    public Messaging(User currentUser)
	{
        this.byUser = currentUser;
	}

    public int SendMessage(string toUsername, string message)
    {
        try
        {
            // create a new message
            BugComment newMessage = new BugComment();
            newMessage.CreatedDate = DateTime.Now;
            newMessage.CreatedUser = byUser.Username;
            newMessage.ToUser = toUsername;
            newMessage.Message = message; //TODO: be sure to sanitize HTML!

            // insert into database
            ApplicationGlobalVariables.KillDaBugsDatabase.BugComments.InsertOnSubmit(newMessage);

            // if everything alright
            return newMessage.BugCommentId;
        }
        catch (Exception ex)
        {
            return -1;
            throw ex;
        }
    }

    public int SendMessage(string toUsername, int bugId, string message)
    {
        try
        {
            // create a new message
            BugComment newMessage = new BugComment();
            newMessage.CreatedDate = DateTime.Now;
            newMessage.CreatedUser = byUser.Username;
            newMessage.ToUser = toUsername;
            newMessage.BugId = bugId;
            newMessage.Message = message; //TODO: be sure to sanitize HTML!

            // insert into database
            ApplicationGlobalVariables.KillDaBugsDatabase.BugComments.InsertOnSubmit(newMessage);

            // if everything alright
            return newMessage.BugCommentId;
        }
        catch (Exception ex)
        {
            return -1;
            throw ex;
        }
    }

    public int SendMessage(int bugId, string message)
    {
        try
        {
            // create a new message
            BugComment newMessage = new BugComment();
            newMessage.CreatedDate = DateTime.Now;
            newMessage.CreatedUser = byUser.Username;
            newMessage.BugId = bugId;
            newMessage.Message = message; //TODO: be sure to sanitize HTML!

            // insert into database
            ApplicationGlobalVariables.KillDaBugsDatabase.BugComments.InsertOnSubmit(newMessage);

            // if everything alright
            return newMessage.BugCommentId;
        }
        catch (Exception ex)
        {
            return -1;
            throw ex;
        }
    }

    public int AddAttachment(int bugCommentId, string filename, string fileExtension, float filesize)
    {
        try
        {
            // create a new attachment
            BugCommentAttachment newAttachment = new BugCommentAttachment();

            // save new field values
            newAttachment.BugCommentId = bugCommentId;
            newAttachment.Filename = filename;
            newAttachment.FileExtension = fileExtension;
            newAttachment.Filesize = filesize;

            // add new attachment to database;
            ApplicationGlobalVariables.KillDaBugsDatabase.BugCommentAttachments.InsertOnSubmit(newAttachment);
            ApplicationGlobalVariables.KillDaBugsDatabase.SubmitChanges();

            // if everything alright
            return newAttachment.AttachmentId;
        }
        catch (Exception ex)
        {
            return -1;
            throw ex;
        }
    }
}